package com.zmovie.common.utils;

import com.zmovie.common.error.BusinessException;
import com.zmovie.common.error.EnumBusinessError;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 文件上传/删除
 * @since 2023-11-28 21:04
 */
@Slf4j
public class FileUtils {
    //路径前缀 映射到磁盘
    public static final String PROJECT_FILE_PREFIX = "/cert";
    //文件保存路径
    public static final String ROOT = "E:/zmovie";
    public static final String actorPath = "/actor";
    public static final String cinemaPath = "/cinema";
    public static final String moviePath = "/movie";
    public static final String userPath = "/user";

    /**
     * 处理文件路径。
     * @param filePath 格式为 E:\\zomvoie\\**
     * @return /zmovie/**
     */
    public static String dealFilePath(String filePath){
        //E:\\zomvie -> /
        String path = filePath.replace(ROOT.replace("/", "\\"), "/");
        path = path.replace("\\", "/");
        return PROJECT_FILE_PREFIX + path;
    }

    public static void main(String[] args) {
        String filePath = "E:\\zmovie\\movie\\2c54e670-d53a-4bac-92c2-b3e5f9a7ea38.jpg";
        System.out.println(dealFilePath(filePath));
    }

    /**
     * 保存文件到本地，返回文件名列表
     * @param files 文件集
     */
    public static List<String> saveFile(List<MultipartFile> files, String path){
        List<String> fileNameList = new ArrayList<>();
        if(files == null || files.size() < 1) return fileNameList;
        for(MultipartFile file : files){
            String fileName = FileUtils.generateRandomFileName(file.getOriginalFilename());
            //保存到服务器
            fileName = FileUtils.upload(file, path, fileName);
            fileNameList.add(fileName); //保存文件名
        }
        return fileNameList;
    }

    public static String upload(MultipartFile file, String destPath, String fileName) {
        File dest = new File(ROOT +destPath + File.separator + fileName);
        //判断文件父目录是否存在
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            //保存文件
            file.transferTo(dest);
        } catch (Exception e) {
            log.error("Save file exception. {}", e.getMessage());
        }
        return dest.getPath();
    }
    public static boolean delete(List<String> fileNameList) throws BusinessException {
        for(String fileName : fileNameList){
            if(!delete(fileName)) return false;
        }
        return true;
    }

    public static boolean delete(String fileName) throws BusinessException {
        File file = null;
        try {
            file = new File(fileName);
        } catch (Exception e) {
            throw new BusinessException(EnumBusinessError.OPERATOR_ERROR);
        }
        return file.delete();
    }

    public static String getSuffix(String fileName) {
        return fileName.substring(fileName.lastIndexOf("."));
    }

    public static String generateRandomFileName(String fileName) {
        return UUID.randomUUID() + getSuffix(fileName);
    }


}
